package org.example.service;


import org.example.entity.User;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 定制消息组件
 */
@Service
public class MessageProducerService {

    private AmqpAdmin amqpAdmin;
    @Autowired
    private RabbitTemplate rabbitTemplate;
/**
 * 可省略 在@RabbitListener(bindings = @QueueBinding(value = @Queue("router.error.queue"),
 *             exchange = @Exchange(value = "routing_exchange", type = "direct"), key = "error_routing_key"))
 *             已创建相关交换机和消息队列以及绑定
 */
//    @Autowired
//    public MessageProducerService(AmqpAdmin amqpAdmin, RabbitTemplate rabbitTemplate) {
//        this.amqpAdmin = amqpAdmin;

//        this.rabbitTemplate = rabbitTemplate;
    //new FanoutExchange 创建广播交换机
//        this.amqpAdmin.declareExchange(new FanoutExchange("pub/sub.exchange"));
//        this.amqpAdmin.declareQueue(new Queue("email.queue"));
//        this.amqpAdmin.declareQueue(new Queue("sms.queue"));
//        //把队列绑定在交换机上
//        this.amqpAdmin.declareBinding(new Binding("email.queue", Binding.DestinationType.QUEUE, "pub/sub.exchange", "", null));
//        this.amqpAdmin.declareBinding(new Binding("sms.queue", Binding.DestinationType.QUEUE, "pub/sub.exchange", "", null));
//
//    }

    /**
     * 路由模式
     * @param amqpAdmin
     * @param rabbitTemplate
     */
//    @Autowired
//    public MessageProducerService(AmqpAdmin amqpAdmin, RabbitTemplate rabbitTemplate) {
//        this.amqpAdmin = amqpAdmin;
//        this.rabbitTemplate = rabbitTemplate;
//        //new DirectExchange 创建直接交换机
//        this.amqpAdmin.declareExchange(new DirectExchange("routing_exchange"));
//        this.amqpAdmin.declareQueue(new Queue("router.error.queue"));
//        this.amqpAdmin.declareQueue(new Queue("routing_queue_all"));
//        //把队列绑定在交换机上
//        this.amqpAdmin.declareBinding(new Binding("router.error.queue", Binding.DestinationType.QUEUE, "routing_exchange", "", null));
//        this.amqpAdmin.declareBinding(new Binding("routing_queue_all", Binding.DestinationType.QUEUE, "routing_exchange", "", null));
//
//    }


    /**
     * 路由模式
     *
     * @param amqpAdmin
     * @param rabbitTemplate
     */
//    @Autowired
//    public MessageProducerService(AmqpAdmin amqpAdmin, RabbitTemplate rabbitTemplate) {
//        this.amqpAdmin = amqpAdmin;
//        this.rabbitTemplate = rabbitTemplate;
//        //new DirectExchange 创建直接交换机
//        this.amqpAdmin.declareExchange(new TopicExchange("topic_exchange"));
//        this.amqpAdmin.declareQueue(new Queue("topic.email.queue"));
//        this.amqpAdmin.declareQueue(new Queue("topic.sms.queue"));
//        //把队列绑定在交换机上
//        this.amqpAdmin.declareBinding(new Binding("topic.email.queue", Binding.DestinationType.QUEUE, "topic_exchange", "", null));
//        this.amqpAdmin.declareBinding(new Binding("topic.sms.queue", Binding.DestinationType.QUEUE, "topic_exchange", "", null));
//
//    }
    public void psubPublisher(User user) {
        this.rabbitTemplate.convertAndSend("pub/sub.exchange", "", user);
    }


    public void routingPublisher(String message) {
        rabbitTemplate.convertAndSend("routing_exchange",
                "error_routing_key",
                message);

    }

    public void routingPublisher2(String message) {
        rabbitTemplate.convertAndSend("routing_exchange",
                "info_routing_key",
                message);

    }

    public void routingPublisher3(String key, String message) {
        rabbitTemplate.convertAndSend("routing_exchange",
                key,
                message);

    }

    public void TopicPublisher(String routingKey, String message) {
        rabbitTemplate.convertAndSend("topic_exchange",
                routingKey,
                message);

    }
}
